﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.Linq;
using QuanLyKhachSan.report;
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;
using CrystalDecisions.Windows.Forms;
namespace QuanLyKhachSan.BaoCao
{
    public partial class frmBaoCaoDoanhThuNgay : DevExpress.XtraEditors.XtraForm
    {
        DataClassesKhachSanDataContext db = new DataClassesKhachSanDataContext(SystemInfo.pub_ConnString);
        public frmBaoCaoDoanhThuNgay()
        {
            InitializeComponent();
        }
        ParameterField paraField;
        ParameterFields paraFields = new ParameterFields();
        ParameterDiscreteValue paraDiscreteValue;
        public void PassParameter(string parameterFieldName, string parameterDiscreteValue, CrystalReportViewer viewer)
        {
            paraField = new ParameterField();
            paraField.Name = parameterFieldName;//tên c?a parameter
            paraDiscreteValue = new ParameterDiscreteValue();
            paraDiscreteValue.Value = parameterDiscreteValue;//gán giá tr? cho parameter
            paraField.CurrentValues.Add(paraDiscreteValue);
            paraFields.Add(paraField);
            viewer.ParameterFieldInfo = paraFields;
        }

        private void btBaoCao_Click(object sender, EventArgs e)
        {
            this.filldata();   
        }
        void filldata()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("P_MA");
            dt.Columns.Add("P_ID");
            dt.Columns.Add("CTTP_DONGIA");
            dt.Columns.Add("CTTP_TIENPHONG", typeof(Decimal));
            dt.Columns.Add("CTTP_NGAYBD");
            dt.Columns.Add("CTTP_NGAYKT");
            dt.Columns.Add("KH_LOAI");
            dt.Columns.Add("CTTP_TRATRUOC", typeof(Decimal));
            dt.Columns.Add("CTTP_GHICHU");
            dt.Columns.Add("THUOC LA", typeof(Int16));
            dt.Columns.Add("tong THUOC LA", typeof(Decimal));
            dt.Columns.Add("NHA HANG", typeof(Int16));
            dt.Columns.Add("tong nha hang", typeof(Decimal));
            dt.Columns.Add("XE", typeof(Int16));
            dt.Columns.Add("tong xe", typeof(Decimal));
            dt.Columns.Add("GIAT UI", typeof(Int16));
            dt.Columns.Add("tong giat ui", typeof(Decimal));
            dt.Columns.Add("SUOI", typeof(Int16));
            dt.Columns.Add("tong SUOI", typeof(Decimal));
            dt.Columns.Add("CHANH", typeof(Int16));
            dt.Columns.Add("tong CHANH", typeof(Decimal));
            dt.Columns.Add("NGOT", typeof(Int16));
            dt.Columns.Add("tong NGOT", typeof(Decimal));
            dt.Columns.Add("BEER", typeof(Int16));
            dt.Columns.Add("SoNgay");
            dt.Columns.Add("tong BEER" , typeof (Decimal));

            var d = db.DoanhThuNgay(Convert.ToDateTime(dateEdit1.EditValue)).ToList();
            Int16 tongsuoiViet = Convert.ToInt16(d.Where(i => i.KH_LOAI == "Việt Nam").Sum(f => f.SUOI).Value);
            Int16 tongchanhViet = Convert.ToInt16(d.Where(i => i.KH_LOAI == "Việt Nam").Sum(f => f.CHANH).Value);
            Int16 tongngotViet = Convert.ToInt16(d.Where(i => i.KH_LOAI == "Việt Nam").Sum(f => f.NGOT).Value);
            Int16 tongbiaViet = Convert.ToInt16(d.Where(i => i.KH_LOAI == "Việt Nam").Sum(f => f.BEER).Value);
            Int16 tongsuoiTay = Convert.ToInt16(d.Where(i => i.KH_LOAI == "Nước Ngoài").Sum(f => f.SUOI).Value);
            Int16 tongchanhTay = Convert.ToInt16(d.Where(i => i.KH_LOAI == "Nước Ngoài").Sum(f => f.CHANH).Value);
            Int16 tongngotTay = Convert.ToInt16(d.Where(i => i.KH_LOAI == "Nước Ngoài").Sum(f => f.NGOT).Value);
            Int16 tongbiaTay = Convert.ToInt16(d.Where(i => i.KH_LOAI == "Nước Ngoài").Sum(f => f.BEER).Value);
            


            for (int i = 0; i < d.Count; i++)
            {
                Double songay = 0;
                if (d[i].CTTP_NGAYKT != null)
                {
                    TimeSpan ngay = DateTime.Parse(d[i].CTTP_NGAYKT.ToString().Substring(0, 10)) - DateTime.Parse(d[i].CTTP_NGAYBD.ToString().Substring(0, 10));
                    songay = ngay.TotalDays;
                }

                dt.Rows.Add( d[i].P_MA,
                     d[i].P_ID,
                      d[i].CTTP_DONGIA,
                       d[i].CTTP_TIENPHONG,
                        d[i].CTTP_NGAYBD,
                         d[i].CTTP_NGAYKT,
                          d[i].KH_LOAI,
                           d[i].CTTP_TRATRUOC,
                            d[i].CTTP_GHICHU,
                             d[i].THUOC_LA,
                              d[i]._tong_THUOC_LA,
                               d[i].NHA_HANG,
                                d[i]._tong_nha_hang,
                                 d[i].XE,
                                  d[i]._tong_xe,
                                   d[i].GIAT_UI,
                                    d[i]._tong_giat_ui,
                                     d[i].SUOI,
                                      d[i]._tong_SUOI_,
                                       d[i].CHANH,
                                        d[i]._tong_CHANH_,
                                         d[i].NGOT,
                                          d[i]._tong_NGOT_,
                                           d[i].BEER,
                                            songay,
                                             d[i]._tong_BEER_         
                    ); 
            }

            rptBaoCaoDoanhThuNgay doanhthu = new rptBaoCaoDoanhThuNgay();
            doanhthu.SetDataSource(dt);
            crystalReportViewer1.ReportSource = doanhthu;


            this.PassParameter("SuoiViet", tongsuoiViet.ToString(), crystalReportViewer1);
            this.PassParameter("ChanhViet", tongchanhViet.ToString(), crystalReportViewer1);
            this.PassParameter("NgotViet", tongngotViet.ToString(), crystalReportViewer1);
            this.PassParameter("BiaViet", tongbiaViet.ToString(), crystalReportViewer1);
            this.PassParameter("SuoiTay", tongsuoiTay.ToString(), crystalReportViewer1);
            this.PassParameter("ChanhTay", tongchanhTay.ToString(), crystalReportViewer1);
            this.PassParameter("NgotTay", tongngotTay.ToString(), crystalReportViewer1);
            this.PassParameter("BiaTay", tongbiaTay.ToString(), crystalReportViewer1);
            this.PassParameter("LeTan", SystemInfo.pub_AppUserFullName , crystalReportViewer1);
        }
    }
}